<template>
  <div class="page-header-index-wide page-header-wrapper-grid-content-main">
    <a-row :gutter="24">
      <a-col :md="24" :lg="4" style="padding: 0px 0px ; padding-left: 6px; padding-right: 0px;">

        <div class="recommended-books">
          <div class="title">
            <span>📚 图书榜</span>
          </div>
          <ul>
            <li class="book-item">
              <a href="/books/18" target="_blank">
                <img class="book-item-img" src="https://img.golang123.com/upload/img/2018/06/5f111fe1-f6f3-4cb0-a0eb-6cb0458e206a.jpg">
                <div class="book-item-info">
                  <div class="book-item-title">Node.js 调试指南</div>
                  <div>
                    <span class="book-item-buycount">9人已学习</span>
                    <span class="try-read">阅读</span>
                  </div>
                </div>
              </a>
            </li>
            <li class="book-item">
              <a href="/books/43" target="_blank">
                <img class="book-item-img" src="https://img.golang123.com/upload/img/2018/04/cb4e1ba4-9fb5-44f4-9873-50672dc7478e.jpg">
                <div class="book-item-info">
                  <div class="book-item-title">ECMAScript 6 入门</div>
                  <div>
                    <span class="book-item-buycount">5人已学习</span>
                    <span class="try-read">阅读</span>
                  </div>
                </div>
              </a>
            </li>
          </ul>
        </div>

        <div class="recommended-authors">
          <div class="title">
            <span style="color: #333;">🎖️ 作者榜</span>
          </div>
          <ul class="list">

            <li class="item" :key="index" v-for=" (item , index) in newAuthors ">
              <div class="link">
                <a @click="$router.push(`/account/author/${item.username}`)" target="_blank" class="recommended-avatar" :style="`background-image: url(${item.avatar});`"></a>
                <div class="user-info">
                  <a @click="$router.push(`/account/author/${item.username}`)" target="_blank" class="username">{{item.realname}}</a>
                  <a target="_blank" class="rank">
                    <img :src="`https://cdn.jsdelivr.net/gh/Miazzy/yunwisdoms@v3.0.0/images/${item.level}.svg`">
                  </a>
                  <div class="position"></div>
                  <div class="description">{{ item.bio }}</div>
                </div>
              </div>
            </li>
            
            <li class="item">
              <a @click="$router.push(`/account/authors/index`);" target="_blank" class="more">
                <span>完整榜单</span>
                <i class="fa fa-angle-right" style="font-size: 16px;"></i>
              </a>
            </li>
          </ul>
        </div>

        <div class="sidebar-block related-entry-sidebar-block shadow" st:block="relatedEntrySidebarBlock">
          <div class="block-title">🔥  最新文章</div>

          <div class="block-body">
            <div class="entry-list" :key="index" v-for="(item , index) in newBlogData ">
              <a @click="handleBlogView(item)" target="_blank" rel="" st:name="link" class="item" >
                <div class="entry-title" style="cursor: pointer;"><a>{{ item.title }}</a></div>
                <div class="entry-meta-box">
                  <div class="entry-meta"><img src="//cdn.jsdelivr.net/gh/Miazzy/yunwisdoms@v3.0.0/images/zan.svg" class="icon"><span class="count">{{ item.star }}</span></div>
                  <div class="entry-meta"><img src="//cdn.jsdelivr.net/gh/Miazzy/yunwisdoms@v3.0.0/images/comment.svg" class="icon"><span class="count">{{ item.messages }}</span></div>
                </div>
              </a>
            </div>
          </div>
        </div>

      </a-col>

      <a-col :md="24" :lg="20">
        <a-card
          style="width:100%"
          :bordered="false"
          :tabList="tabListNoTitle"
          :activeTabKey="noTitleKey"
          @tabChange="key => handleTabChange(key, 'noTitleKey')"
        >
          <article-page v-if="noTitleKey === '推荐文章'"></article-page>
          <article-page v-if="noTitleKey === '社会聚焦'"></article-page>
          <article-page v-if="noTitleKey === '科学技术'"></article-page>
          <article-page v-if="noTitleKey === '人工智能'"></article-page>
          <article-page v-if="noTitleKey === '诸子百家'"></article-page>
          <article-page v-if="noTitleKey === '金融管理'"></article-page>
          <article-page v-if="noTitleKey === '建筑工程'"></article-page>
          <article-page v-if="noTitleKey === '艺术设计'"></article-page>
          <article-page v-if="noTitleKey === '团队文化'"></article-page>
          <article-page v-if="noTitleKey === '问答交流'"></article-page>
          <article-page v-if="noTitleKey === '前端开发'"></article-page>
          <article-page v-if="noTitleKey === 'Android'"></article-page>
          <article-page v-if="noTitleKey === 'IOS开发'"></article-page>
          <article-page v-if="noTitleKey === '后端开发'"></article-page>
        </a-card>
      </a-col>

    </a-row>
  </div>
</template>

<script>
import PageLayout from "@/components/page/PageLayout";
import RouteView from "@/components/layouts/RouteView";
import { ArticlePage } from "./page";

import * as manageAPI from "@/api/manage";
import * as storage from "@/utils/storage";
import * as tools from "@/utils/util";
import * as authortool from "@/api/blogAPI/author";

export default {
  components: {
    RouteView,
    PageLayout,
    ArticlePage,
  },
  data() {
    return {
      tags: [],
      postName: "",
      departName: "",
      address: "",
      bio: "",
      tagInputVisible: false,
      tagInputValue: "",
      tabListNoTitle: JSON.parse(`[{"key":"推荐文章","tab":"推荐文章","index":0},{"key":"社会聚焦","tab":"社会聚焦","index":1},{"key":"科学技术","tab":"科学技术","index":2},{"key":"人工智能","tab":"人工智能","index":3},{"key":"诸子百家","tab":"诸子百家","index":4},{"key":"金融管理","tab":"金融管理","index":5},{"key":"建筑工程","tab":"建筑工程","index":6},{"key":"艺术设计","tab":"艺术设计","index":7},{"key":"团队文化","tab":"团队文化","index":8},{"key":"问答交流","tab":"问答交流","index":9},{"key":"前端开发","tab":"前端开发","index":10},{"key":"Android","tab":"Android","index":11},{"key":"IOS开发","tab":"IOS开发","index":12},{"key":"后端开发","tab":"后端开发","index":13}]`),
      fdata: null,
      userinfo: null,
      v_user: null,
      newAuthors:[],
      newBlogData:[],
      nowAvatars:[],
      nowAvatarMap: new Map(),
      noTitleKey: "推荐文章"
    };
  },
  computed: {
    userInfo() {
      return this.$store.getters.userInfo;
    }
  },
  async created() {

    //获取用户信息
    this.user = this.userInfo;

    //设置员工岗位信息/部门信息
    try {
      storage.setStore(`system_title_key`,this.noTitleKey);

      this.v_user = await manageAPI.queryUserInfoByView(this.user.username);

      this.postName = this.v_user[0]["post"];
      this.departName = this.v_user[0]["name"];
      this.address = this.v_user[0]["address"];
      this.bio = this.v_user[0]["bio"];
      this.avatar = this.v_user[0]["avatar"];

      this.v_user[0]["tags"] = this.v_user[0]["tags"].replace(/，/g, ",");
      this.tags = this.v_user[0]["tags"].split(",");

      this.newBlogData = await manageAPI.queryBlogInfo(0,5);
      this.newAuthors = await this.queryAuthorRank(0,3);
      this.nowAvatars = await this.queryAvatarAll();

      for(let avatar of this.nowAvatars){
        this.nowAvatarMap.set(avatar.username , `${window._CONFIG['uploadURL']}/${avatar.avatar}`);
      }
      
      await window.articleLoadData(window.blogArticle, storage, manageAPI);

    } catch (error) {
      console.log("工作台设置员工岗位信息/部门信息异常：" + error);
    }
  },
  async mounted() {
    await this.handleArticleStyle();
  },
  methods: {
    ...Vuex.mapGetters(["nickname", "avatar"]),
    /**
     * @function 处理切换书签功能
     */
    async handleTabChange(key, type) {
      try {
        this[type] = key;
        storage.setStore(`system_title_key`,key);
        await window.articleLoadData(window.blogArticle, storage, manageAPI);
      } catch (error) {
        console.log(error);
      }
      try {
        var title = this.tabListNoTitle.find((item) => {
          return item.key == key;
        })
        await this.handleArticleStyle(key , title);
      } catch (error) {
        console.log(error);
      }
    },
    /**
     * @function 查询用户榜单
     */
    async queryAuthorRank(page = 0 , size = 10 ){
      return await authortool.queryAuthorRank(page , size , storage , tools);
    },
    /**
     * @function 查询用户头像
     */
    async queryAvatarAll(page = 0, size = 99, type = 'username', times = 101, expiretime = 3600 * 24 * 1.5){
      return await authortool.queryAvatarAll(page , size , type , times , storage, tools, expiretime);
    },
    /**
     * @function 处理博文预览功能
     */
    async handleBlogView(item) {
      try {
        //标签
        var tags = tools.deNull(item.tags);
        //跳转到博文详情页面
        this.$router.push(
          `/blog/view?id=${item.id}&author=${item.create_by}&tags=${tags}`
        );
      } catch (error) {
        console.log("$router go to error :" + error);
      }
    },
    async handleArticleStyle(key , title , index){
      try {
        index = title.index * 128 * 0.86;
        await this.handleChangeCss(index);
      } catch (error) {
        console.log(error);
      }
      setTimeout(async () => {
        await this.handleChangeCss(index);
      },100);

    },
    async handleChangeCss(index){
      try {
        $('.page-header-wrapper-grid-content-main .ant-tabs-tab-active').css('cssText', 'color: #1890ff; font-weight: 500; border-bottom: 3px solid #1890ff;');
        $('.page-header-wrapper-grid-content-main .ant-tabs-tab').css('cssText', 'margin: 0 14px 0 0;');
        $('.page-header-wrapper-grid-content-main .ant-tabs-ink-bar-animated').css('cssText',`display: block;transform: translate3d(${index}px, 0px, 0px);width: 96px;`);
      } catch (error) {
        console.log(error);
      }
    },
    async handleTagClose(removeTag) {
      const tags = this.tags.filter(tag => tag != removeTag);
      this.tags = tags;

      try {
        //待保存数据
        this.fdata = { tags: tags.toString() };

        //获取用户信息
        this.v_user = await manageAPI.queryUserInfoByView(
          this.userInfo.username
        );

        //此次设置用户岗位
        await manageAPI.patchTableData(
          "sys_user",
          this.v_user[0].id,
          this.fdata
        );

        //属性合并
        this.v_user[0] = Object.assign({}, this.v_user[0], this.fdata);

        //用户信息修改，修改缓存信息
        storage.setStore(
          `system_v_user_info@username$${this.userInfo.username}`,
          this.v_user,
          3600 * 24
        );
      } catch (error) {
        console.log(error);
      }
    },

    async showTagInput() {
      this.tagInputVisible = true;
      this.$nextTick(() => {
        this.$refs.tagInput.focus();
      });
    },

    async handleInputChange(e) {
      this.tagInputValue = e.target.value;
    },

    async handleTagInputConfirm() {
      //获取输入数据
      const inputValue = this.tagInputValue;

      //定义tags内容
      var tags = this.tags;

      try {
        //将新输入的tag加入tags
        if (inputValue && tags.indexOf(inputValue) === -1) {
          tags = [...tags, inputValue];
        }
      } catch (error) {
        console.log(error);
      }

      try {
        //待保存数据
        this.fdata = { tags: tags.toString() };

        //获取用户信息
        this.v_user = await manageAPI.queryUserInfoByView(
          this.userInfo.username
        );

        //此次设置用户岗位
        await manageAPI.patchTableData(
          "sys_user",
          this.v_user[0].id,
          this.fdata
        );

        //属性合并
        this.v_user[0] = Object.assign({}, this.v_user[0], this.fdata);

        //用户信息修改，修改缓存信息
        storage.setStore(
          `system_v_user_info@username$${this.userInfo.username}`,
          this.v_user,
          3600 * 24
        );
      } catch (error) {
        console.log(error);
      }

      try {
        Object.assign(this, {
          tags,
          tagInputVisible: false,
          tagInputValue: ""
        });
      } catch (error) {
        console.log(error);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
a {
    text-decoration: none;
    cursor: pointer;
    color: #909090;
}

.page-header-wrapper-grid-content-main {
  width: 100%;
  height: 100%;
  min-height: 100%;
  transition: 0.3s;

  a {
    text-decoration: none;
    cursor: pointer;
    color: #606060;
  }

  a:hover { 
    text-decoration: none;
    cursor: pointer;
    color: #1890FF;
  }

  .recommended-books {
    padding: 0;
  }

  .sidebar-block:not(.pure) {
    background-color: #fff;
  }

  .sidebar-block {
    position: relative;
    margin-bottom: 1.5rem;
    border-radius: 2px;
  }

  .sidebar-block:not(.pure) .block-title {
    padding: 1rem 1.3rem;
    font-size: 14px;
    margin-left: 3px;
    color: #333;
    border-bottom: 1px solid hsla(0,0%,58.8%,.1);
  }

  .item {
    display: block;
    cursor: pointer;
    padding: .8rem 1.3rem;
  }

  .entry-title {
    font-size: 1.16rem;
    color: #333;
    cursor: pointer;
  }

  .entry-title:hover { 
    cursor: pointer;
  }

  .entry-meta-box {
    margin-top: .4rem;
  }

  .entry-meta {
    display: inline-block;
    margin-right: 1.5rem;
    font-size: 1.1rem;
    color: #c2c2c2;
  }

  .entry-meta .icon {
    vertical-align: sub;
  }

  .entry-meta .count {
    color: #b2bac2;
    margin-left: .4rem;
  }

  .recommended-authors .title, .recommended-books .title {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid hsla(0,0%,58.8%,.1);
  }

  .recommended-authors, .recommended-books {
      border-radius: 2px;
      overflow: hidden;
      background: #fff;
      margin-bottom: 20px;
      font-size: 13px;
      text-align: center;
      box-shadow: 0 1px 2px 0 rgba(0,0,0,.05);
  }

  .recommended-authors .title, .recommended-books .title {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid hsla(0,0%,58.8%,.1);
  }

  .recommended-authors .title span, .recommended-books .title span {
    font-size: 14px;
    color: #333;
  }

  .recommended-authors .item .link {
    padding: 12px 16px;
    display: flex;
    align-items: center;
  }

  .recommended-authors .recommended-avatar {
    display: inline-block;
    position: relative;
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: #eee;
    flex: 0 0 auto;
    width: 46px;
    height: 46px;
    border-radius: 50%;
    margin-right: 10px;
  }

  .recommended-authors .user-info {
    overflow: hidden;
    text-align: left;
  }

  .recommended-authors .username {
    font-size: 14px;
    font-weight: 400;
    color: #333;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 3px;
  }

  .recommended-authors .rank {
    margin-left: 4px;
    vertical-align: middle;
  }

  .recommended-authors .link .description, .recommended-authors .link .position {
    color: #909090;
    font-size: 12px;
    margin-bottom: 3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .recommended-authors .link .description, .recommended-authors .link .position {
    color: #909090;
    font-size: 12px;
    margin-bottom: 3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .recommended-authors .item .more {
    display: block;
    color: #ea6f5a;
    padding: 12px 0;
    text-align: center;
    border-top: 1px solid hsla(0,0%,59.2%,.1);
  }

  .recommended-books .book-item {
    padding: 10px;
    overflow: hidden;
    margin-bottom: 0;
  }

  .recommended-authors, .recommended-books {
      border-radius: 2px;
      overflow: hidden;
      background: #fff;
      margin-bottom: 20px;
      font-size: 13px;
      text-align: center;
      box-shadow: 0 1px 2px 0 rgba(0,0,0,.05);
  }

  .recommended-books .book-item a {
    display: block;
    overflow: hidden;
  }

  .recommended-books .book-item-info {
    float: left;
    max-width: 148px;
    min-width: 130px;
    margin-left: 10px;
    text-align: left;
  }

  .recommended-books .book-item-title {
    color: #333;
    font-weight: 500;
    font-size: 14px;
    margin-bottom: 5px;
  }

  .book-item-buycount {
    color: #878c90;
    float: left;
    height: 20px;
    line-height: 20px;
  }

  .try-read {
    height: 20px;
    line-height: 20px;
    color: #fff;
    font-size: 12px;
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 2px;
    background-color: #007fff;
    float: right;
  }

  *, :after, :before {
      box-sizing: border-box;
  }

  .recommended-books .book-item-img {
    float: left;
    width: 52px;
    height: 72px;
  }

  .ant-tabs-ink-bar-animated{
    display:none;
  }

  .ant-tabs-tab-active {
      color: #1890ff;
      font-weight: 500;
      border-bottom: 3px solid #1890ff;
  }

  .ant-tabs-tab {
      position: relative;
      display: inline-block;
      box-sizing: border-box;
      height: 100%;
      margin: 0 20px 0 0;
      padding: 12px 16px;
      text-decoration: none;
      cursor: pointer;
      transition: color .3s cubic-bezier(.645,.045,.355,1);
  }

  .account-center-avatarHolder {
    text-align: center;
    margin-bottom: 24px;

    & > .avatar {
      margin: 0 auto;
      width: 104px;
      height: 104px;
      margin-bottom: 20px;
      border-radius: 50%;
      overflow: hidden;
      img {
        height: 100%;
        width: 100%;
      }
    }

    .username {
      color: rgba(0, 0, 0, 0.85);
      font-size: 20px;
      line-height: 28px;
      font-weight: 500;
      margin-bottom: 4px;
    }
  }

  .account-center-detail {
    p {
      margin-bottom: 8px;
      padding-left: 26px;
      position: relative;
    }

    i {
      position: absolute;
      height: 14px;
      width: 14px;
      left: 0;
      top: 4px;
      background: url(https://gw.alipayobjects.com/zos/rmsportal/pBjWzVAHnOOtAUvZmZfy.svg);
    }

    .title {
      background-position: 0 0;
    }
    .group {
      background-position: 0 -22px;
    }
    .address {
      background-position: 0 -44px;
    }
  }

  .account-center-tags {
    .ant-tag {
      margin-bottom: 8px;
    }
  }

  .account-center-team {
    .members {
      a {
        display: block;
        margin: 12px 0;
        line-height: 24px;
        height: 24px;
        .member {
          font-size: 14px;
          color: rgba(0, 0, 0, 0.65);
          line-height: 24px;
          max-width: 100px;
          vertical-align: top;
          margin-left: 12px;
          transition: all 0.3s;
          display: inline-block;
        }
        &:hover {
          span {
            color: #1890ff;
          }
        }
      }
    }
  }

  .tagsTitle,
  .teamTitle {
    font-weight: 500;
    color: rgba(0, 0, 0, 0.85);
    margin-bottom: 12px;
  }
}
</style>